﻿using DAL;
using IBLL;
using IDAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class PatientBLL : IPatientBLL
    {
        IPatientDAL _dal;

        public PatientBLL(IPatientDAL dal)
        {
            _dal = dal;
        }
        /// <summary>
        /// 新增患者信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public SuccessBase AddPatient(Patient model)
        {
            SuccessBase sb = new SuccessBase();
            if (model.Patient_Name == null || model.Patient_Name == "")
            {
                sb.Code = MyEnum.验证未通过;
                sb.Success = false;
                sb.Message = "请输入患者姓名";
                return sb;
            }
            var res = _dal.AddPatient(model);
            if (res > 0)
            {
                sb.Code = MyEnum.请求成功;
                sb.Success = true;
                sb.Message = "添加成功";
            }
            else
            {
                sb.Code = MyEnum.验证未通过;
                sb.Success = false;
                sb.Message = "添加失败";
            }
            return sb;
        }
        /// <summary>
        /// 查询科室信息
        /// </summary>
        /// <returns></returns>
        public List<Outpatient> GetOutpatient()
        {
            return _dal.GetOutpatient();
        }

        /// <summary>
        /// 查询患者信息
        /// </summary>
        /// <returns></returns>
        public PageModel GetPatient(string str, int pageIndex = 1, int pageSize = 10)
        {
            PageModel model = new PageModel();

            model.patient = new List<PatientTwo>();

            var list = _dal.GetPatient(str);

            var count = list.Count;

            if (count % pageSize == 0)
            {
                count = count / pageSize;
            }
            else
            {
                count = count / pageSize + 1;
            }
            model.patient = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            model.pageCount = count;

            return model;
        }
        /// <summary>
        /// 根据id查询单条数据（编辑详情）
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public Patient GetPatientById(int pid)
        {
            return _dal.GetPatientById(pid);
        }
        /// <summary>
        /// 修改患者信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public SuccessBase UpdatePatient(Patient model)
        {
            SuccessBase sb = new SuccessBase();

            var res = _dal.UpdatePatient(model);

            if (res > 0)
            {
                sb.Success = true;
                sb.Code = MyEnum.请求成功;
                sb.Message = "修改成功";
            }
            else
            {
                sb.Success = false;
                sb.Code = MyEnum.验证未通过;
                sb.Message = "修改失败";
            }
            return sb;
        }
        /// <summary>
        /// 根据id修改患者就诊状态
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public SuccessBase UpdatePatientById(int pid, int state)
        {
            SuccessBase sb = new SuccessBase();
            int res = _dal.UpdatePatientById(pid, state);
            if (res > 0)
            {
                sb.Code = MyEnum.请求成功;
                sb.Success = true;
                sb.Message = "就诊完成";
            }

            return sb;
        }
    }
}
